package com.egoo.ticket.server.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.EwsUserDao;
import com.egoo.ticket.server.dao.WorkOrderDao;
import com.egoo.ticket.server.dao.WorkOrderStatusDao;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.entity.WorkOrderStatus;
import com.egoo.ticket.server.service.WorkOrderStatusService;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WorkOrderStatusServiceImpl implements WorkOrderStatusService {
	@Autowired
	private WorkOrderStatusDao workOrderStatusDao;

	@Autowired
	private EwsUserDao ewsUserDao;
	
	@Autowired
	private WorkOrderDao workOrderDao;

	/**
	 * TODO 根据状态Id获取工单状态信息
	 * @param id
	 * @return
	 */
	@Override
	public WorkOrderStatus selectById(Integer id,String tenantId) {
		WorkOrderStatus workOrderStatus = workOrderStatusDao.selectById(id,tenantId);
		if(null == workOrderStatus){
			workOrderStatus = new WorkOrderStatus();
			workOrderStatus.setId(id);
			workOrderStatus.setName("状态不变");
		}
		return workOrderStatus;
	}

	/**
	 * TODO 根据状态名称获取工单状态信息
	 *
	 * @param name
	 * @return
	 */
	@Override
	public DbResponse selectByName(String name, Integer currentPage, Integer pageSize,
								   String tenantId) {
		PageHelper.startPage(currentPage,pageSize);
		List<WorkOrderStatus> workOrderStatusList = workOrderStatusDao.selectByName(name,tenantId);
		PageInfo<WorkOrderStatus> workOrderStatusPageInfo = new PageInfo<>(workOrderStatusList);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG,workOrderStatusList,
				workOrderStatusPageInfo.getTotal());
	}

	/**
	 * TODO 新建工单状态
	 *
	 * @param
	 * @return
	 */
	@Override
	public Integer insertStatus(String name, Integer ordering, String userId,String tenantId) {
		WorkOrderStatus workOrderStatus = new WorkOrderStatus();

		//获取当前用户信息
		EwsUser ewsUser = ewsUserDao.selectById(userId);
		String ewsUserName = ewsUser.getName();

		workOrderStatus.setName(name);
		workOrderStatus.setTag(0);
		Timestamp time = new Timestamp(System.currentTimeMillis());

		workOrderStatus.setOrdering(ordering);
		workOrderStatus.setCreateUserId(userId);
		workOrderStatus.setCreateUserName(ewsUserName);
		workOrderStatus.setCreateTime(time);
		workOrderStatus.setLastUserId(userId);
		workOrderStatus.setLastUserName(ewsUserName);
		workOrderStatus.setTenantId(tenantId);
		workOrderStatus.setLastUpdateTime(time);
		workOrderStatus.setId(((Long)System.currentTimeMillis()).intValue());
		return workOrderStatusDao.insertStatus(workOrderStatus);

	}

	/**
	 *TODO  编辑工单状态
	 *
	 * @param workOrderStatus
	 * @param userId
	 * @return
	 */
	@Override
	public Integer updateStatus(WorkOrderStatus workOrderStatus, String userId) {
		EwsUser ewsUser = ewsUserDao.selectById(userId);
		workOrderStatus.setLastUserId(userId);
		workOrderStatus.setLastUserName(ewsUser.getName());
		Timestamp time = new Timestamp(System.currentTimeMillis());
		workOrderStatus.setLastUpdateTime(time);
		Integer result = workOrderStatusDao.updateStatus(workOrderStatus);
		return result;
	}

	/**
	 *TODO  删除工单状态
	 *
	 * @param id
	 * @return
	 */
	@Override
	public Integer deleteStatus(Integer id,String tenantId) {
		WorkOrderStatus workOrderStatus = workOrderStatusDao.selectById(id,tenantId);
		return workOrderStatusDao.deleteStatus(workOrderStatus);
	}

	/**
	 * TODO 返回当前排序最大值+1
	 *
	 * @return
	 */
	@Override
	public Integer selectOrderMax(String tenantId) {
		Integer count = 0;
		List<WorkOrderStatus> workOrderStatusList = workOrderStatusDao.selectAll(tenantId);
		if (null != workOrderStatusList && !workOrderStatusList.isEmpty()) {
			count = workOrderStatusDao.selectOrderMax() + 1;
			return count;
		}
		count = count + 1;
		return count;
	}

	/**
	 * TODO 通过状态排序获取工单状态信息
	 * @param odering
	 * @return
	 */
	@Override
	public WorkOrderStatus selectByOrdering(Integer odering) {
		WorkOrderStatus workOrderStatus = workOrderStatusDao.selectOrdering(odering);
		return workOrderStatus;
	}

	/**
	 * TODO 根据状态名称（不是模糊查询）获取工单状态名称
	 * @param name
	 * @return
	 */
	@Override
	public WorkOrderStatus selectByEqualsName(String name,String tenantId) {
		WorkOrderStatus workOrderStatus = workOrderStatusDao.selectByEqualsName(name,tenantId);
		return workOrderStatus;
	}

	/**
	 *TODO  获取所有工单状态信息
	 *
	 * @return
	 */
	@Override
	public List<WorkOrderStatus> selectAll(String tenantId) {
		return workOrderStatusDao.selectAll(tenantId);
	}

	/**
	 * TODO 分页查询
	 *
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<WorkOrderStatus> listPageAllWorkOrderStatus(Integer currentPage, Integer pageSize,String tenantId) {

		return workOrderStatusDao.selectAllByCount((currentPage - 1) * pageSize, pageSize,tenantId);
	}

	/**
	 * TODO 根据状态名称获取工单状态信息(不包含状态不变)
	 * @param name
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@Override
	public DbResponse selectByNameNotContainNotNoChanging(String name,
														  Integer currentPage, Integer pageSize, String tenantId) {
		PageHelper.startPage(currentPage,pageSize);
		List<WorkOrderStatus> workOrderStatusList = workOrderStatusDao.selectByNameNotContainNotNoChanging(name,tenantId);
		PageInfo<WorkOrderStatus> workOrderStatusPageInfo = new PageInfo<>(workOrderStatusList);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG,workOrderStatusList,
				workOrderStatusPageInfo.getTotal());
	}

	/**
	 *  TODO 获取工单类型节点操作按钮
	 * @param
	 * @return
	 */
	@Override
	public JSONArray getWorkTypeNodeButton(Integer statusId) {
		String jsonObject = workOrderDao.selectWorkTypeByStatusId(statusId);
		JSONObject parseObject = JSONObject.parseObject(jsonObject);
		return parseObject.getJSONArray("sequenceFlows");
	}


}

